﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace HQWeb.View
{
    public partial class GetThemeView : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                string strID = Request.Form["id"];
                hidID.Value = "6";
                BindView();
                // Universal.JS.RunJS("getSearch()");
            }
        }
        //绑定数据
        private void BindView()
        {
            //try
            //{
            string strBranchID = Session["BRANCH_ID"].ToString();
            DataTable dt = GetFile(strBranchID);
            int intCoount = dt.Rows.Count;
            Photocount.Value = intCoount.ToString();
            //count.InnerText = intCoount.ToString() + "记录   当前第一页";
            intCoount = intCoount < 16 ? intCoount : 16;
            StringBuilder strHtml = new StringBuilder();
            DataRow rowTemp;
            string strTitle;
            for (int i = 0; i < intCoount; i++)
            {
                rowTemp = dt.Rows[i];
                strTitle = rowTemp["title"].ToString();
                strHtml.Append("<li class=\"list\" onmousemove='mson(this)' onmouseout='msout(this)'>");
                strHtml.Append("<div class=\"p-img\">");

                strHtml.Append(string.Format("<a target=\"_blank\" onclick=\"ShowTeme('{0}','{1}','{2}','{3}','{4}')\">", strTitle, rowTemp["purl"], rowTemp["vurl"], rowTemp["id"], rowTemp["ourl"]));
                strHtml.Append(" <img onerror=\"this.src='http://www.360buy.com/images/none/none_150.gif'\" ");
                strHtml.Append(string.Format("alt=\"{0}\"", strTitle));
                strHtml.Append(string.Format("width=\"280px\" height=\"150px\" src=\"../{0}\"></a>", rowTemp["surl"]));

                strHtml.Append("<div class=\"pi7\">");
                strHtml.Append(" </div>");
                strHtml.Append("</div>");
                strHtml.Append("<div class=\"p-name\">");
                strHtml.Append("<font");
                strHtml.Append("class=\"adwords\" name=\"577394\">" + strTitle + "</font></div>");
                strHtml.Append("</li>");
            }
            PageIndex.Value = "1";
            countList.InnerHtml = strHtml.ToString();

            BLL.SysManage.T_PUB_BRANCH branchBLL = new BLL.SysManage.T_PUB_BRANCH();
            Model.SysManage.T_PUB_BRANCH branchModel = branchBLL.GetModel(Convert.ToInt32(strBranchID));
            if (branchModel != null)
            {
                hidIP.Value = branchModel.NAS_USERID + ":" + branchModel.NAS_PASSWORD + "@" + branchModel.NAS_IP;
            }
            else
            {

            }
            //}
            //catch (Exception ex)
            //{

            //    Universal.JS.Alert(ex.Message);
            //}
        }

        private DataTable GetFile(string strBranchID)
        {

            BLL.SysManage.T_PUB_WEDDINGTHEME themeBll = new BLL.SysManage.T_PUB_WEDDINGTHEME();
            DataTable dt = themeBll.GetThemmFile(strBranchID, "", "", "", "", "", "");
            DataTable newTable = new DataTable();
            newTable.Columns.Add("id", typeof(string));
            newTable.Columns.Add("title", typeof(string));
            newTable.Columns.Add("surl", typeof(string));
            newTable.Columns.Add("purl", typeof(string));
            newTable.Columns.Add("vurl", typeof(string));
            newTable.Columns.Add("ourl", typeof(string));
            int coutn = dt.Rows.Count;
            DataRow rowTemp;
            DataRow[] findRow;
            string strid;
            string strType;
            string strURLOriginal;
            string strURLThumbnail;
            for (int i = 0; i < coutn; i++)
            {
                rowTemp = dt.Rows[i];
                strid = rowTemp["ID"].ToString();
                strType = rowTemp["TYPE"].ToString();
                strURLOriginal = rowTemp["URLOriginal"].ToString().Replace("//", "/");
                strURLThumbnail = rowTemp["URLThumbnail"].ToString().Replace("//", "/");
                findRow = newTable.Select(string.Format(" id='{0}'", strid));
                if (findRow.Length > 0)
                {
                    if (strType == "0")     //图片
                    {
                        findRow[0]["purl"] = findRow[0]["purl"] + strURLOriginal + ",";
                        findRow[0]["surl"] = strURLThumbnail;
                    }
                    else
                    {
                        findRow[0]["vurl"] = strURLOriginal;
                    }

                }
                else
                {
                    DataRow newRows = newTable.NewRow();
                    newRows["ID"] = strid;
                    newRows["title"] = rowTemp["TITLE"];
                    newRows["ourl"] = rowTemp["FILE_URL"];
                    if (strType == "0")     //图片
                    {
                        newRows["purl"] = strURLOriginal + ",";
                        newRows["surl"] = strURLThumbnail;
                    }
                    else
                    {
                        newRows["vurl"] = strURLOriginal;
                    }
                    newTable.Rows.Add(newRows);
                }
            }
            return newTable;
        }
    }
}